package xzy.Controller;

import lombok.SneakyThrows;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
import xzy.dao.GradeMapper;
import xzy.dao.StudentMapper;
import xzy.pojo.Grade;
import xzy.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Resgist_Servlet")
public class Resgist_Servlet extends HttpServlet {
    private static ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    StudentMapper studentMapper= (StudentMapper) context.getBean("studentMapper");

    @SneakyThrows
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入学生注册账号页面");
        request.setCharacterEncoding("utf-8");
            String userName=request.getParameter("userName");
            String password=request.getParameter("password");
            try{
                if(userName.length()>0&&password.length()>0){
                    Student student=new Student(userName,password);
                    int res=studentMapper.addStudent(student);
                    if(res>0){
                        request.setAttribute("StudentResgist_error","注册学生账号成功");
                    }else {
                        request.setAttribute("StudentResgist_error","注册学生账号失败");
                    }
                }else {
                    request.setAttribute("StudentResgist_error","注册用户名，密码不可以为空");
                }
            }catch (Exception e){
                request.setAttribute("StudentResgist_error","系统已有此用户");
            }finally {
                request.getRequestDispatcher("/StudentResgist.jsp").
                        forward(request,response);
            }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
